package org.genew.yop.ejb;

import java.util.Collection;

import javax.annotation.Resource;
import javax.ejb.LocalBean;
import javax.ejb.Remote;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.genew.squadra.yop.model.Canzoni;
import org.genew.squadra.yop.model.Gruppo;
import org.genew.squadra.yop.model.Locale;
import org.genew.squadra.yop.model.Utente;

/**
 * Session Bean implementation class Manager
 */
@Stateless
@Remote(ManagerRemote.class)
public class Manager implements ManagerRemote {

	@PersistenceContext(unitName="Yop-domain")
	private EntityManager em;
	
	@Resource
	private SessionContext etx;
	
	
    /**
     * Default constructor. 
     */
    public Manager() {
        // TODO Auto-generated constructor stub
    }
    
    public Collection<Gruppo> findAllGruppi(){
    	
    	Query query = em.createQuery(""
    			+ "select g ,u, ge, gg, f, af "
    			+ "from gruppo g, utente u, genere ge, gruppo_genere gg, foto f, album_fotografico af  "
    			+ "where g.id = u.id"
    			+ "and gg.gruppo_id = g.id"
    			+ "and gg.genere_id = ge.id"
    			+ "and u.id = af.utente_id"
    			+ "and f.albumfotografico_id = f.id"
    			+ "and f.tag = 'profile'");
    	Collection<Gruppo> gruppi = (Collection<Gruppo>) query.getResultList();
		return gruppi;
    	
    }
    

    public Canzoni findCanzoniById(int canzoniID){
		Query query = em.createQuery("select c from Canzoni c where c.id =:canzoniID");
		query.setParameter("canzoniID", canzoniID);
		Canzoni  c = (Canzoni)query.getSingleResult();
		
    	return c;
    	
    }

	@Override
	public Collection<Gruppo> findAllGruppiByPosition(float lat, float lng) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Gruppo findGruppoById(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Collection<Locale> findAllLocali() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Collection<Locale> findAllLocaliByPosition(float lat, float lng) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Locale findLocaleById(int id) {
		// TODO Auto-generated method stub
		return null;
	}
}
